package com.wlxk.crawler.core.service.impl;

import com.wlxk.crawler.core.entity.ChapterDO;
import com.wlxk.crawler.core.enums.SourceEnum;
import com.wlxk.crawler.core.exception.WlxkErrEnum;
import com.wlxk.crawler.core.exception.WlxkException;
import com.wlxk.crawler.core.service.ISyncService;
import com.wlxk.crawler.core.util.HttpClientDownloader;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Spider;

/**
 * @description:
 * @author: 21054523
 * @time: 2023/10/26 9:39
 */
@Service
public class SyncServiceImpl implements ISyncService {

    @Override
    @Async
    public void syncChapter(ChapterDO chapterDO) {
        SourceEnum sourceEnum = SourceEnum.getByKey(chapterDO.getSource());
        if (null == sourceEnum) {
            throw new WlxkException(WlxkErrEnum.UNKNOWN);
        }
        Spider.create(sourceEnum.getPageProcessor()).addPipeline(sourceEnum.getPipelineBean())
                .setDownloader(new HttpClientDownloader()).addUrl(chapterDO.getUrl()).thread(1).run();
    }
}
